package com.uvnos4j.matilda.security.callback;

import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.commons.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证成功回调
 * <p>
 * Description:认证成功回调
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @see com.uvnos4j.matilda.security.callback
 * @since 2020-08-16 23:46:22
 */
@Slf4j
public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        log.info("[Authentication]\r\n用户登录成功，用户信息：\r\n{}。", authentication);
        if (RequestUtil.isAjax()) {
            ResponseUtil.writeJson(Result.success(authentication));
        } else {
            super.onAuthenticationSuccess(request, response, authentication);
        }
    }

}